package com.ct.application.sysapis.service.impl;

import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.sysapis.model.vo.SysApisVO;
import com.ct.application.sysapis.request.SysApisBatchRequest;
import com.ct.application.sysapis.request.SysApisSaveRequest;
import com.ct.application.sysapis.request.SysApisSearchRequest;
import com.ct.application.sysapis.request.SysApisUpdateRequest;
import com.ct.application.sysapis.service.ISysApisService;
import com.ct.rbac.enums.RbacResultEnum;
import com.ct.rbac.model.SysApisModel;
import com.ct.rbac.orm.SysApisDO;
import com.ct.rbac.repository.ISysApisRespontory;
import com.ct.rbac.repository.ISysRoleApisRespontory;
import com.ct.repository.convert.CopyConvertUtils;
import com.ct.utils.exception.ApiException;
import com.ct.utils.result.StatusEnums;
import com.ct.utils.valid.ValidatorUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@Service
public class SysApisServiceImpl implements ISysApisService {

    @Resource
    private ISysApisRespontory sysApisRespontory;
    @Resource
    private ISysRoleApisRespontory sysRoleApisRespontory;

    @Override
    public List<SysApisVO> findPageMenu(SysApisSearchRequest sysApisSearchRequest) {
        List<SysApisModel> respontoryRoles = sysApisRespontory.findPageMenu(sysApisSearchRequest.getStatus(), sysApisSearchRequest.getKeyword());
        return tranferListBo(respontoryRoles, SysApisVO.class);
    }
    /**
     * API列表--租户使用
     * @return
     */
    @Override
    public List<SysApisVO> findSystemPageMenu(Long roleId) {
        List<SysApisModel> respontoryRoles = sysApisRespontory.findSystemPageMenu(roleId);
        return tranferListBo(respontoryRoles, SysApisVO.class);
    }

    @Override
    public SysApisVO selectById(Long id) {
        SysApisDO sysApisDO = sysApisRespontory.getById(id);
        return tranferBo(sysApisDO, SysApisVO.class);
    }

    @Override
    public Boolean deleteById(Long id) {
        // 如果角色被用户绑定了。直接返回
        if(sysRoleApisRespontory.countApiRole(id)){
            throw new ApiException(RbacResultEnum.API_YINYONG_API_EX);
        }
        return sysApisRespontory.removeById(id);
    }

    @Override
    public SysApisVO saveSysApis(SysApisSaveRequest sysApisSaveRequest) {
        SysApisDO sysApisDO = CopyConvertUtils.copyProperties(sysApisSaveRequest, SysApisDO.class);
        Boolean b1 = sysApisRespontory.countByCode(sysApisSaveRequest.getCode(),null);
        if(b1){
            throw new ApiException(RbacResultEnum.CODE_API_EXSITS_EX);
        }
        Boolean b2 = sysApisRespontory.countByPath(sysApisSaveRequest.getPath(),null);
        if(b2){
            throw new ApiException(RbacResultEnum.PATH_API_EXSITS_EX);
        }
        boolean b = sysApisRespontory.save(sysApisDO);
        if (b){
            return CopyConvertUtils.copyProperties(sysApisDO, SysApisVO.class);
        }
        return null;
    }

    @Override
    public SysApisVO updateSysApis(SysApisUpdateRequest sysApisUpdateRequest) {
        SysApisDO sysApisDO = CopyConvertUtils.copyProperties(sysApisUpdateRequest, SysApisDO.class);
        Boolean b1 = sysApisRespontory.countByCode(sysApisUpdateRequest.getCode(),sysApisUpdateRequest.getId());
        if(b1){
            throw new ApiException(RbacResultEnum.CODE_API_EXSITS_EX);
        }
        Boolean b2 = sysApisRespontory.countByPath(sysApisUpdateRequest.getPath(),sysApisUpdateRequest.getId());
        if(b2){
            throw new ApiException(RbacResultEnum.PATH_API_EXSITS_EX);
        }

        boolean b = sysApisRespontory.updateById(sysApisDO);
        if (b){
            return CopyConvertUtils.copyProperties(sysApisDO, SysApisVO.class);
        }
        return null;
    }

    @Override
    public Boolean updateStatusSysApis(StatusRequest statusRequest) {
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return sysApisRespontory.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return sysApisRespontory.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }

    @Override
    public Boolean batchStatus(SysApisBatchRequest sysApisBatchRequest) {
        List<Long> userIds = sysApisBatchRequest.getIds();
        List<SysApisDO> sysApisDOS = userIds.stream().map(userId -> {
            SysApisDO sysApisDO = new SysApisDO();
            sysApisDO.setId(userId);
            sysApisDO.setStatus(StatusEnums.STATUS.getCode());
            return sysApisDO;
        }).collect(Collectors.toList());
        return sysApisRespontory.updateBatchById(sysApisDOS);
    }

    @Override
    public Boolean batchForbiddens(SysApisBatchRequest sysApisBatchRequest) {

        List<Long> userIds = sysApisBatchRequest.getIds();
        List<SysApisDO> sysApisDOS = userIds.stream().map(userId -> {
            SysApisDO sysApisDO = new SysApisDO();
            sysApisDO.setId(userId);
            sysApisDO.setStatus(StatusEnums.STATUS.getCode());
            return sysApisDO;
        }).collect(Collectors.toList());
        return sysApisRespontory.updateBatchById(sysApisDOS);
    }
}

